package com.sherlockHolmes.init.controller;

import com.sherlockHolmes.init.base.result.Ajax;
import com.sherlockHolmes.init.entity.dao.UserInfo;
import com.sherlockHolmes.init.service.UserInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/index")
public class LoginController {
    @Resource
    private UserInfoService userService;
    @PostMapping("/login")
    public Ajax login(UserInfo user, HttpServletRequest request) {
        if ((user.getUserName() == null|| user.getUserName().equals(""))
                || (user.getPassword() == null|| user.getPassword().equals(""))) {
            return Ajax.fail("用户名或密码不能为空");
        }
        try {
            String login = userService.login(user, request);
            return Ajax.success(login);
        }catch (Exception e){
            return Ajax.fail(e);
        }
    }
    /**
     * 注册
     */
    @PostMapping("/register")
    public Ajax register(UserInfo user) {
       try{
           return Ajax.success(userService.register(user));
       }catch (Exception e){
           return Ajax.fail(e);
       }
    }
}
